package com.ccrfid.rmc6.resource.toolcase;

import com.ccrfid.rmc6.appconfig.exception.pojo.AppException;
import com.ccrfid.rmc6.entity.toolcase.ToolCaseEntity;
import com.ccrfid.rmc6.entity.toolcase.ToolEntity;
import com.ccrfid.rmc6.entity.toolcase.ToolTakeRecordEntity;
import com.ccrfid.rmc6.entity.toolcase.ToolTakeRecordView;
import com.ccrfid.rmc6.pojo.dto.LocatorAssetCount;
import com.ccrfid.rmc6.pojo.param.PageParam;
import com.ccrfid.rmc6.repository.search.Criterion;
import com.ccrfid.rmc6.repository.search.SearchCriteria;
import com.ccrfid.rmc6.service.toolcase.ToolCaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.sql.Timestamp;
import java.util.List;

@Path("tool-case")
@Produces(MediaType.APPLICATION_JSON)
public class ToolCaseResource {

    @Autowired
    private ToolCaseService toolCaseService;

    //分页查询工具箱列表
    @GET
    @Path("page")
    public Page<ToolCaseEntity> getToolCasePage(@QueryParam("cardNumber") Long cardNumber,
                                                @QueryParam("enabled") Integer enabled,
                                                @QueryParam("takeOut") Integer takeOut,
                                                @QueryParam("stationary") Integer stationary,
                                                @QueryParam("personnelName") String personnelName,
                                                @QueryParam("toolCaseSn") String toolCaseSn,
                                                @QueryParam("toolCaseName") String toolCaseName,
                                                @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (stationary != null) {
            criteria.add(new Criterion("stationary", Criterion.Operator.EQ, stationary));
        }
        if (cardNumber != null) {
            criteria.add(new Criterion("cardNumber", Criterion.Operator.EQ, cardNumber));
        }
        if (enabled != null) {
            criteria.add(new Criterion("enabled", Criterion.Operator.EQ, enabled));
        }
        if (takeOut != null) {
            criteria.add(new Criterion("takeOut", Criterion.Operator.EQ, takeOut));
        }
        if (personnelName != null) {
            criteria.add(new Criterion("personnelName", Criterion.Operator.LIKE, '%' + personnelName + '%'));
        }
        if (toolCaseSn != null) {
            criteria.add(new Criterion("toolCaseSn", Criterion.Operator.LIKE, '%' + toolCaseSn + '%'));
        }
        if (toolCaseName != null) {
            criteria.add(new Criterion("toolCaseName", Criterion.Operator.LIKE, '%' + toolCaseName + '%'));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return toolCaseService.getToolCasePage(criteria, pageRequest);
    }

    //获取某个工具箱
    @GET
    @Path("{toolCaseId}")
    public ToolCaseEntity getToolCaseById(@PathParam("toolCaseId") Long toolCaseId) throws AppException {
        return toolCaseService.getToolCaseById(toolCaseId);
    }

    @GET
    @Path("{cardNumber}/by-card-number")
    public ToolCaseEntity getToolCaseByCardNumber(@PathParam("cardNumber") Long cardNumber) throws AppException {
        return toolCaseService.getToolCaseByCardNumber(cardNumber);
    }

    //新增工具箱
    @POST
    @Path("")
    @Consumes(MediaType.APPLICATION_JSON)
    public ToolCaseEntity createToolCase(ToolCaseEntity entity) throws AppException {
        return toolCaseService.createToolCase(entity);
    }

    //更新工具箱信息
    @PUT
    @Path("{toolCaseId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public ToolCaseEntity updateToolCase(@PathParam("toolCaseId") Long toolCaseId, ToolCaseEntity entity) throws AppException {
        return toolCaseService.updateToolCase(toolCaseId, entity);
    }

    //删除某个工具箱
    @DELETE
    @Path("{toolCaseId}")
    public Boolean deleteToolCase(@PathParam("toolCaseId") Long toolCaseId) throws AppException {
        return toolCaseService.deleteToolCase(toolCaseId);
    }

    //获取某个工具箱内的全部工具
    @GET
    @Path("{toolCaseId}/tools")
    public List<ToolEntity> getToolsInToolCase(@PathParam("toolCaseId") Long toolCaseId) throws AppException {
        return toolCaseService.getToolsInToolCase(toolCaseId);
    }

    //为某个工具箱添加工具
    @POST
    @Path("{toolCaseId}/tools")
    @Consumes(MediaType.APPLICATION_JSON)
    public ToolEntity createTool(@PathParam("toolCaseId") Long toolCaseId, ToolEntity toolEntity) throws AppException {
        return toolCaseService.createTool(toolCaseId, toolEntity);
    }

    //修改某个工具信息
    @PUT
    @Path("{toolCaseId}/tools/{toolId}")
    @Consumes(MediaType.APPLICATION_JSON)
    public ToolEntity updateTool(@PathParam("toolCaseId") Long toolCaseId,
                                 @PathParam("toolId") Long toolId,
                                 ToolEntity toolEntity) throws AppException {
        return toolCaseService.updateTool(toolCaseId, toolId, toolEntity);
    }

    //删除某个工具信息
    @DELETE
    @Path("{toolCaseId}/tools/{toolId}")
    public Boolean deleteTool(@PathParam("toolCaseId") Long toolCaseId,
                              @PathParam("toolId") Long toolId) throws AppException {
        return toolCaseService.deleteTool(toolCaseId, toolId);
    }

    //工具箱借出
    @PUT
    @Path("{toolCaseId}/take-out")
    @Consumes(MediaType.APPLICATION_JSON)
    public Boolean toolCaseTakeOut(@PathParam("toolCaseId") Long toolCaseId, ToolTakeRecordEntity entity) throws AppException {
        return toolCaseService.toolCaseTakeOut(toolCaseId, entity);
    }

    //工具箱归还
    @PUT
    @Path("{toolCaseId}/take-back")
    public Boolean toolCaseTakeBack(@PathParam("toolCaseId") Long toolCaseId) throws AppException {
        return toolCaseService.toolCaseTakeBack(toolCaseId);
    }

    //工具箱借出记录分页查询
    @GET
    @Path("take-out-records")
    public Page<ToolTakeRecordView> getToolCaseTakeRecords(@QueryParam("cardNumber") Long cardNumber,
                                                           @QueryParam("enabled") Integer enabled,
                                                           @QueryParam("takeOut") Integer takeOut,
                                                           @QueryParam("stationary") Integer stationary,
                                                           @QueryParam("personnelName") String personnelName,
                                                           @QueryParam("toolCaseId") Long toolCaseId,
                                                           @QueryParam("toolCaseSn") String toolCaseSn,
                                                           @QueryParam("toolCaseName") String toolCaseName,
                                                           @QueryParam("borrowerName") String borrowerName,
                                                           @QueryParam("startTime") Timestamp startTime,
                                                           @QueryParam("stopTime") Timestamp stopTime,
                                                           @BeanParam PageParam pageParam) {
        SearchCriteria criteria = SearchCriteria.Builder.create();
        if (stationary != null) {
            criteria.add(new Criterion("stationary", Criterion.Operator.EQ, stationary));
        }
        if (cardNumber != null) {
            criteria.add(new Criterion("cardNumber", Criterion.Operator.EQ, cardNumber));
        }
        if (enabled != null) {
            criteria.add(new Criterion("enabled", Criterion.Operator.EQ, enabled));
        }
        if (takeOut != null) {
            criteria.add(new Criterion("takeOut", Criterion.Operator.EQ, takeOut));
        }
        if (personnelName != null) {
            criteria.add(new Criterion("personnelName", Criterion.Operator.LIKE, '%' + personnelName + '%'));
        }
        if (toolCaseId != null) {
            criteria.add(new Criterion("toolCaseId", Criterion.Operator.EQ, toolCaseId));
        }
        if (toolCaseSn != null) {
            criteria.add(new Criterion("toolCaseSn", Criterion.Operator.LIKE, '%' + toolCaseSn + '%'));
        }
        if (toolCaseName != null) {
            criteria.add(new Criterion("toolCaseName", Criterion.Operator.LIKE, '%' + toolCaseName + '%'));
        }
        if (borrowerName != null) {
            criteria.add(new Criterion("borrowerName", Criterion.Operator.LIKE, '%' + borrowerName + '%'));
        }
        if (startTime != null) {
            criteria.add(new Criterion("takeOutTime", Criterion.Operator.GTE, startTime));
        }
        if (stopTime != null) {
            criteria.add(new Criterion("takeOutTime", Criterion.Operator.LTE, stopTime));
        }
        PageRequest pageRequest;
        if (pageParam.getOrders() != null && pageParam.getOrders().size() > 0) {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize(), Sort.by(pageParam.getOrders()));
        } else {
            pageRequest = PageRequest.of(pageParam.getPageNumber(), pageParam.getPageSize());
        }
        return toolCaseService.getToolCaseTakeRecords(criteria, pageRequest);
    }

    //查看工具箱分布
    @GET
    @Path("distribution")
    public List<LocatorAssetCount> getToolCaseDistribution(@QueryParam("floorNumber") Integer floorNumber) {

        return toolCaseService.getToolCaseDistribution(floorNumber);
    }

    //查询某个定位器下的工具箱信息
    @GET
    @Path("under-locator/{locatorNumber}")
    public List<ToolCaseEntity> getToolCaseUnderLocator(@PathParam("locatorNumber") Integer locatorNumber) {
        return toolCaseService.getToolCaseUnderLocator(locatorNumber);
    }

    //查询某个位置下的工具箱信息
    @GET
    @Path("under-locator/{x},{y}")
    public List<ToolCaseEntity> getToolCaseUnderLocator(@PathParam("x") double x, @PathParam("y") double y) {
        return toolCaseService.getToolCaseUnderPos(x, y);
    }

}
